package com.scut.online_black_market.service.impl;

import com.scut.online_black_market.entity.Personal;
import com.scut.online_black_market.mapper.PersonalMapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;

/**
 * JWT认证服务
 * create by yellowBroke on 2020年11月3日 00点13分
 */
@Component
public class JwtUserDetailService implements UserDetailsService {

    @Resource
    private PersonalMapper personalMapper;

    @Override
    public UserDetails loadUserByUsername(String student_number) throws UsernameNotFoundException {

        Personal personal = personalMapper.selectById(student_number);

        if (personal == null) {
            throw new UsernameNotFoundException("该用户不存在");
        }
        Collection<GrantedAuthority> collection = new ArrayList<>();

        return (new User(student_number, personal.getPassword(), collection));
    }
}
